#include "mainscene.h"

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
{
    //初始化主界面
    initScene();

    //启动游戏
    playGame();

}

MainScene::~MainScene() {}

void MainScene::initScene()
{
    //设置主窗口固定大小
    setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    //设置主窗口标题
    setWindowTitle(GAME_TITLE);
    //设置主窗口图标
    setWindowIcon(QIcon(GAME_ICON));

    //初始化定时器
    m_Timer.setInterval(GAME_RATE);

    //敌机出场时间间隔

    m_recorder = 0;

    //设置随机数种子
    srand((unsigned int)time(NULL));


}

void MainScene::playGame()
{

    //启动背景音乐
    QSound::play(SOUND_BACKGROUND);

    //启动定时器
    m_Timer.start();

    //监听定时器信号
    connect(&m_Timer, &QTimer::timeout, [=](){

        //更新地图坐标
        updatePosition();

        //绘制到屏幕中
        update();

        //敌机出场
        enemyToScene();

        //碰撞检测
        collisionDetection();
    });
}

void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosttion();

    // //测试子弹
    // temp_Bullet.m_Free = false;
    // temp_Bullet.updatePosition();

    //发射子弹
    m_hero.shoot();

    for (int i = 0; i < BULLET_NUM; ++i)
    {
        if (m_hero.m_bullets[i].m_Free == false)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }

    //敌机出场
    for (int i = 0; i < ENEMY_NUM; ++i)
    {
        if (m_enemys[i].m_Free == false)
        {
            m_enemys[i].updatePosition();
        }
    }


    //计算爆炸播放的图片

    for(int i=0; i<BOMB_NUM; i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            m_bombs[i].updateInfo();
        }
    }


}

void MainScene::paintEvent(QPaintEvent *ev)
{
    //绘制地图
    QPainter painter(this);
    painter.drawPixmap(0, m_map.m_map1_posY, m_map.m_map1);
    painter.drawPixmap(0, m_map.m_map2_posY, m_map.m_map2);


    //绘制英雄飞机
    painter.drawPixmap(m_hero.m_X, m_hero.m_Y, m_hero.m_Plane);

    // //绘制子弹
    // painter.drawPixmap(temp_Bullet.m_X, temp_Bullet.m_Y, temp_Bullet.m_Bullet);

    //绘制子弹
    for (int i = 0; i < BULLET_NUM; ++i)
    {
        if (m_hero.m_bullets[i].m_Free == false)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X, m_hero.m_bullets[i].m_Y, m_hero.m_bullets[i].m_Bullet);
        }
    }

    //绘制敌机
    for (int i = 0; i < ENEMY_NUM; ++i)
    {
        if (m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_X, m_enemys[i].m_Y, m_enemys[i].m_enemy);
        }
    }

    //绘制爆炸播放的图片

    for(int i=0; i<BOMB_NUM; i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            m_bombs[i].updateInfo();
            //qDebug() <<m_bombs[i].m_index;
            painter.drawPixmap(m_bombs[i].m_X, m_bombs[i].m_Y, m_bombs[i].m_pixArr[m_bombs[i].m_index]);

        }
    }

}

void MainScene::mouseMoveEvent(QMouseEvent *ev)
{
    int x = ev->x() - m_hero.m_Rect.width()/2;
    int y = ev->y() - m_hero.m_Rect.height()/2;

    if(x<=0)
    {
        x = 0;
    }
    else if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();

    }

    if(y<=0)
    {
        y = 0;
    }
    else if(y>=GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }


    m_hero.setPosition(x, y);

}

void MainScene::enemyToScene()
{
    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder = 0;

    for (int i = 0; i < ENEMY_NUM; ++i)
    {
        //空闲敌机
        if(m_enemys[i].m_Free)
        {
            m_enemys[i].m_Free = false;

            //坐标
            m_enemys[i].m_X = rand()%(GAME_WIDTH - m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;

        }
    }
}

void MainScene::collisionDetection()
{
    //遍历所有非空闲敌机
    for(int i = 0; i<ENEMY_NUM; i++)
    {
        //如果是空闲的飞机，进行下一次循环
        if(m_enemys[i].m_Free)
        {
            continue;
        }

        //遍历所有非空闲的子弹
        for(int j =0; j<BULLET_NUM; j++)
        {
            if(m_hero.m_bullets[j].m_Free)
            {
                continue;
            }

            //如果子弹和飞机相交，发射碰撞，将飞机和子弹状态设置为空闲
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemys[i].m_Free = true;
                m_hero.m_bullets[j].m_Free = true;


                //播放爆炸效果

                for(int k = 1; k<BOMB_NUM; k++)
                {
                    if(m_bombs[k].m_Free)
                    {
                        //播放音效
                        QSound::play(SOUND_BOMB);
                        //空闲的爆炸，可以播放了
                        m_bombs[k].m_Free = false;

                        //更新爆炸坐标
                        m_bombs[k].m_X = m_enemys[i].m_X;
                        m_bombs[k].m_Y = m_enemys[i].m_Y;
                        break;
                    }
                }

            }
        }
    }

}
